package com.obelisco.vista.controles;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import nextapp.echo2.app.Extent;

import com.jgoodies.binding.adapter.echo.ComboBoxAdapter;
import com.jgoodies.binding.adapter.echo.SingleListSelectionAdapter;
import com.jgoodies.binding.adapter.echo.TextComponentConnector;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.list.echo.SelectionInList;
import com.jgoodies.binding.value.ValueModel;

import echopointng.ListBoxEx;
import echopointng.SelectFieldEx;

public class BindingListBox extends ListBoxEx {

	// private ValueModel model = null;
	PropertyAdapter model = null;

	private ComboBoxAdapter adapter = null;

	private SelectionInList selectionInList = null;

	private int ancho;

	public BindingListBox(int ancho) {
		super();
		// TODO Auto-generated constructor stub
		this.ancho = ancho;
		initComponents();
	}

	private void initComponents() {
		setStyleName("Obelisco.ListBox");
		setWidth(new Extent(ancho));
	}

	public BindingListBox(int ancho, List lista, Object object, String property) {
		this(ancho);
		if (model == null) {
			model = new PropertyAdapter(object, property, true);
		} else {
			model.setBean(object);
		}
		selectionInList = new SelectionInList(lista, model);
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList
				.getSelectionIndexHolder()));
	}

	public void setBinding(List lista, Object object, String property) {
		if (model == null) {
			model = new PropertyAdapter(object, property, true);
		} else {
			model.setBean(object);
		}
		selectionInList = new SelectionInList(lista, model);
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList
				.getSelectionIndexHolder()));
	}

	public void setItems(List lista) {
		Collections.sort(lista, new ItemsComparator());
		selectionInList = new SelectionInList(lista);
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList
				.getSelectionIndexHolder()));
	}

}

class ItemsComparator implements Comparator {

	public int compare(Object objeto1, Object objeto2) {
		String string1 = objeto1.toString().toUpperCase();
		String string2 = objeto2.toString().toUpperCase();
		return string1.compareTo(string2);		
	}
}